package com.test.mytest.saolei.my.swt;

import java.util.Random;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;


/*
 * Created on 2004-8-6
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author TdbT
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class Mine {
    Data minedata[][] = new Data[9][9];
    Button minebtn[][] = new Button[9][9];
    Composite mineplace;
    Label label1;
   
    Button start;
    public Mine(Composite composite,Label label1,Button start){
        this.mineplace=composite;
        this.label1=label1;
    
        this.start=start;
    }
    public void initiate(Display display) {
        initiateData();
        setProperty();
        countMine();
        setImage();
        initiateButton(display);

    }

    public void refreshButton(){
        Miner.start=false;
        Miner.countnum=10;
        Miner.time=0;
        label1.setText(Integer.toString(Miner.countnum));
        for(int i=0;i<9;i++)
            for(int j=0;j<9;j++){
                minebtn[i][j].setSelection(false);
                minebtn[i][j].setImage(null);
                minebtn[i][j].redraw();
            }
    }
    public void refreshData(){
        for(int i=0;i<9;i++)
            for(int j=0;j<9;j++){
                minedata[i][j].property=0;
                minedata[i][j].minenum=0;
                minedata[i][j].leftclicknum=0;
                minedata[i][j].rightclicknum=0;
                minedata[i][j].setImage(null);
            }
        setProperty();
        countMine();
        setImage();
    }
    void initiateData() {
        Miner.countnum=10;
        Miner.start=false;
        Miner.time=0;
        label1.setText(Integer.toString(Miner.countnum));
        for (int i = 0; i < 9; i++)
            for (int j = 0; j < 9; j++)
                minedata[i][j] = new Data();
    }
    void setProperty(){
        int index1 = 0;
        int index2 = 0;
        Random rand1 = new Random(System.currentTimeMillis());
        for (int i = 0; i < 10; i++) {
            lable1: while (true) {
                index1 = rand1.nextInt(9);
                index2 = rand1.nextInt(9);
                if (minedata[index1][index2].property != 1) {
                    minedata[index1][index2].property = 1;
                    break;
                } else
                    continue lable1;
            }

        }
  }
  
    void initiateButton(Display display) {
        mineplace.setBounds(0,45,152,190);
        for (int i = 0; i < 9; i++)
            for (int j = 0; j < 9; j++) {
                minebtn[i][j] = new Button(mineplace, SWT.TOGGLE);
                minebtn[i][j].setSize(16, 16);
                minebtn[i][j].setLocation(j * 16, i * 16);
                minebtn[i][j].addSelectionListener(new LeftClick(this, 
display, i, j,start));
                minebtn[i][j].addMouseListener(new 
RightClick(minedata[i][j],
                        display, minebtn[i][j],label1,start));
            }
    }
    
    void countMine() {
        for (int i = 0; i < 9; i++)
            for (int j = 0; j < 9; j++) {
                if (i - 1 >= 0 && j - 1 >= 0 && i + 1 < 9 && j + 1 < 9) {
                    if (minedata[i - 1][j - 1].isMine())
                        minedata[i][j].setMineNum();
                    if (minedata[i - 1][j].isMine())
                        minedata[i][j].setMineNum();
                    if (minedata[i - 1][j + 1].isMine())
                        minedata[i][j].setMineNum();
                    if (minedata[i][j - 1].isMine())
                        minedata[i][j].setMineNum();
                    if (minedata[i][j + 1].isMine())
                        minedata[i][j].setMineNum();
                    if (minedata[i + 1][j - 1].isMine())
                        minedata[i][j].setMineNum();
                    if (minedata[i + 1][j].isMine())
                        minedata[i][j].setMineNum();
                    if (minedata[i + 1][j + 1].isMine())
                        minedata[i][j].setMineNum();
                }
                if (i - 1 < 0 && j - 1 < 0) {
                    if (minedata[i][j + 1].isMine())
                        minedata[i][j].setMineNum();
                    if (minedata[i + 1][j].isMine())
                        minedata[i][j].setMineNum();
                    if (minedata[i + 1][j + 1].isMine())
                        minedata[i][j].setMineNum();
                }
                if (i + 1 >= 9 && j - 1 < 0) {
                    if (minedata[i - 1][j].isMine())
                        minedata[i][j].setMineNum();
                    if (minedata[i - 1][j + 1].isMine())
                        minedata[i][j].setMineNum();
                    if (minedata[i][j + 1].isMine())
                        minedata[i][j].setMineNum();
                }
                if (i - 1 < 0 && j + 1 >= 9) {
                    if (minedata[i][j - 1].isMine())
                        minedata[i][j].setMineNum();
                    if (minedata[i + 1][j - 1].isMine())
                        minedata[i][j].setMineNum();
                    if (minedata[i + 1][j].isMine())
                        minedata[i][j].setMineNum();
                }
                if (i + 1 >= 9 && j + 1 >= 9) {
                    if (minedata[i - 1][j - 1].isMine())
                        minedata[i][j].setMineNum();
                    if (minedata[i - 1][j].isMine())
                        minedata[i][j].setMineNum();
                    if (minedata[i][j - 1].isMine())
                        minedata[i][j].setMineNum();
                }
                if (j - 1 < 0 && i - 1 >= 0 && i + 1 < 9) {
                    if (minedata[i - 1][j].isMine())
                        minedata[i][j].setMineNum();
                    if (minedata[i - 1][j + 1].isMine())
                        minedata[i][j].setMineNum();
                    if (minedata[i][j + 1].isMine())
                        minedata[i][j].setMineNum();
                    if (minedata[i + 1][j].isMine())
                        minedata[i][j].setMineNum();
                    if (minedata[i + 1][j + 1].isMine())
                        minedata[i][j].setMineNum();
                }
                if (i - 1 < 0 && j - 1 >= 0 && j + 1 < 9) {
                    if (minedata[i][j - 1].isMine())
                        minedata[i][j].setMineNum();
                    if (minedata[i][j + 1].isMine())
                        minedata[i][j].setMineNum();
                    if (minedata[i + 1][j - 1].isMine())
                        minedata[i][j].setMineNum();
                    if (minedata[i + 1][j].isMine())
                        minedata[i][j].setMineNum();
                    if (minedata[i + 1][j + 1].isMine())
                        minedata[i][j].setMineNum();
                }
                if (i + 1 >= 9 && j - 1 >= 0 && j + 1 < 9) {
                    if (minedata[i - 1][j - 1].isMine())
                        minedata[i][j].setMineNum();
                    if (minedata[i - 1][j].isMine())
                        minedata[i][j].setMineNum();
                    if (minedata[i - 1][j + 1].isMine())
                        minedata[i][j].setMineNum();
                    if (minedata[i][j - 1].isMine())
                        minedata[i][j].setMineNum();
                    if (minedata[i][j + 1].isMine())
                        minedata[i][j].setMineNum();

                }
                if (j + 1 >= 9 && i - 1 >= 0 && i + 1 < 9) {
                    if (minedata[i - 1][j - 1].isMine())
                        minedata[i][j].setMineNum();
                    if (minedata[i - 1][j].isMine())
                        minedata[i][j].setMineNum();
                    if (minedata[i][j - 1].isMine())
                        minedata[i][j].setMineNum();
                    if (minedata[i + 1][j - 1].isMine())
                        minedata[i][j].setMineNum();
                    if (minedata[i + 1][j].isMine())
                        minedata[i][j].setMineNum();
                }
            }

    }

    void setImage() {
        for (int i = 0; i < 9; i++)
            for (int j = 0; j < 9; j++) {
                if (minedata[i][j].isMine())
                    minedata[i][j].setImage("mine1.gif");
                else if (minedata[i][j].getMineNum() == 0)
                    minedata[i][j].setImage("blank.gif");
                else
                    minedata[i][j].setImage(minedata[i][j].getMineNum()
                            + ".gif");
            }
    }
} 